package longrise.phone.com.bjjt_henan.compensate;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.bindinginformation.FeedBackView;

/**
 * 评价界面
 */

public class EvaluateView extends LFView implements View.OnClickListener {

    private Context mContext;

    private View mView;

    private View fView;

    private View sView;

    private EditText contentET;

    private Button commitBtn;

    private String appcaseno;

    private String centerid;

    private BaseApplication mApplication;

    public EvaluateView(Context context, String appcaseno) {
        super(context);
        this.mContext = context;
        this.appcaseno = appcaseno;
        mApplication = ((MainActivity) mContext).getAppData();
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_evaluate, null);
        if (mView == null) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText("服务评价");
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });

        fView = mView.findViewById(R.id.evaluate_layout);
        initRateViews(fView);

        contentET = (EditText) mView.findViewById(R.id.evaluate_et);
        commitBtn = (Button) mView.findViewById(R.id.evaluate_btn);

        setEvent(true);
    }

    private void initViews() {
        if (!TextUtils.isEmpty(centerid)) {
            ViewStub viewStub = (ViewStub) mView.findViewById(R.id.evaluate_vs);
            sView = viewStub.inflate();
            initRateViews(sView);

            initInfoViews(fView, new String[]{"在线理赔", "理赔效率", "服务态度"});
            initInfoViews(sView, new String[]{"服务中心", "服务态度", "维修服务"});
        } else {
            initInfoViews(fView, new String[]{"总体评价", "理赔效率", "服务态度"});
        }
    }

    private void setEvent(boolean flag) {
        if (commitBtn != null) {
            commitBtn.setOnClickListener(flag ? this : null);
        }
    }

    private void initInfoViews(View layout, String[] infos) {
        TextView titleTV = (TextView) layout.findViewById(R.id.evaluate_tv_title);
        titleTV.setText(infos[0]);
        TextView efficTV = (TextView) layout.findViewById(R.id.evaluate_tv_effic);
        efficTV.setText(infos[1]);
        TextView serviceTV = (TextView) layout.findViewById(R.id.evaluate_tv_service);
        serviceTV.setText(infos[2]);
    }

    private void initRateViews(View layout) {
        RatingBar allRBar = (RatingBar) layout.findViewById(R.id.evaluate_rb_all);
        CommontUtils.setRatingBarHeight(mContext, allRBar, R.mipmap.ratingbar_select);
        RatingBar efficRBar = (RatingBar) layout.findViewById(R.id.evaluate_rb_effic);
        CommontUtils.setRatingBarHeight(mContext, efficRBar, R.mipmap.ratingbar_select);
        RatingBar serviceRBar = (RatingBar) layout.findViewById(R.id.evaluate_rb_service);
        CommontUtils.setRatingBarHeight(mContext, serviceRBar, R.mipmap.ratingbar_select);
    }

    private float[] getRates(View layout) {
        float[] rates = new float[3];
        RatingBar allRBar = (RatingBar) layout.findViewById(R.id.evaluate_rb_all);
        float all = allRBar.getRating();
        if (all < 1) {
            all = 5.0f;//默认五星好评
        }
        rates[0] = all;
        RatingBar efficRBar = (RatingBar) layout.findViewById(R.id.evaluate_rb_effic);
        float effic = efficRBar.getRating();
        if (effic < 1) {
            effic = 5.0f;//默认五星好评
        }
        rates[1] = effic;
        RatingBar serviceRBar = (RatingBar) layout.findViewById(R.id.evaluate_rb_service);
        float service = serviceRBar.getRating();
        if (service < 1) {
            service = 5.0f;//默认五星好评
        }
        rates[2] = service;

        return rates;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.evaluate_btn:
                EntityBean bean = new EntityBean();
                bean.set("centerid", centerid);
                bean.set("review", contentET.getText().toString().trim());

                float[] fRates = getRates(fView);
                bean.set("totalscore", fRates[0]);
                bean.set("cpspeed", fRates[1]);
                bean.set("dsreasonable", fRates[2]);

                String isonline;
                if (!TextUtils.isEmpty(centerid)) {
                    float[] sRates = getRates(sView);
                    bean.set("cpcscore", sRates[0]);
                    bean.set("handlespeed", sRates[1]);
                    bean.set("environment", sRates[2]);
                    isonline = "0";
                } else {
                    isonline = "1";
                }
                String uid = mApplication.getKckpName();
                String token = mApplication.getToken();
                DialogUtils.showDialog(mContext);
                ApiImpI.getInstance(mContext).submitEvaluation(appcaseno, bean, isonline, uid, token, new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(mContext, error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        if (mView == null) {
                            return;
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            showReportDialog();
                            return;
                        }
                        String redes = bean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            UiUtil.showToast(mContext, redes);
                        }
                    }
                });

                break;
            default:
                break;
        }
    }

    private void showReportDialog() {
        final Dialog dialog = new Dialog(mContext, R.style.AlertDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_evaluatesuccess, null);
        view.findViewById(R.id.dialog_btn_report).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                FeedBackView view = new FeedBackView(mContext, true);
                FrameworkManager.getInstance().showNewForm(mContext, view);
            }
        });
        dialog.setContentView(view);
        //设置大小
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.80);
        dialogWindow.setAttributes(lp);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                //回到主页
                ((MainActivity) mContext).backToMainView();
            }
        });
        dialog.show();
    }

    @Override
    public void refresh() {
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(mContext.getApplicationContext()).getCenterid(appcaseno, uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (mView == null) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean dataBean = bean.getBean("data");
                    if (dataBean != null) {
                        centerid = dataBean.getString("centerid");
                        initViews();
                        return;
                    }
                }
                String redes = bean.getString("redes");
                if (!TextUtils.isEmpty(redes)) {
                    UiUtil.showToast(mContext, redes);
                }
            }
        });
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setEvent(false);
        mView = null;
    }
}
